package web.servlet;

import domain.Account;
import service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "/ResetServlet", urlPatterns = {"/Reset"})
public class ResetServlet extends HttpServlet {
    private AccountService accountService = new AccountService();
    private String password, repeatPassword;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.password = req.getParameter("password");
        this.repeatPassword = req.getParameter("repeatPassword");
        if(password == null || password.isEmpty()){
            req.setAttribute("resetInfo", "密码不能为空");
            req.getRequestDispatcher("ResetForm").forward(req,resp);
            return;
        }
        if(password.equals(repeatPassword)){
            accountService.changePassword(password, (String) req.getSession().getAttribute("resetUserid"));
            req.getRequestDispatcher("LoginForm").forward(req,resp);
        }else {
            req.setAttribute("resetInfo", "两次输入密码不一致");
            req.getRequestDispatcher("ResetForm").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
